package com.eastfair.gateway.component;

import com.eastfair.core.context.StaticContextUtil;
import com.eastfair.core.utils.NetUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * springboot容器启动完成后运行
 */
@Component
@Slf4j
public class SecurityApplicationRunnerCore implements ApplicationRunner {

    //暂时写死
    private String voPackScan = "com.eastfair";
    private static final String LOCAL = "local";
    private static final String DEV = "dev";
    private static final String TEST = "test";
    private static final String PROD = "prod";

    @Value("${spring.profiles.active}")
    private String profilesActive;

    @Override
    public void run(ApplicationArguments applicationArguments) throws Exception {
        log.info("ApplicationRunnerCore is execute");
        //记录IP地址
        String ip = NetUtils.getLocalAddress();
        StaticContextUtil.setIpAddr(ip);
        //记录是否是开发环境
        if (DEV.equalsIgnoreCase(profilesActive)) {
            StaticContextUtil.setProfilesActive(DEV);
        } else if (TEST.equalsIgnoreCase(profilesActive)) {
            StaticContextUtil.setProfilesActive(TEST);
        } else if (LOCAL.equalsIgnoreCase(profilesActive)) {
            StaticContextUtil.setProfilesActive(LOCAL);
        } else {
            StaticContextUtil.setProfilesActive(PROD);
        }

    }

}
